package com.lbc.demo01;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

//  练习 Thread ， 实现多线程同步下载图片
public class TestThread2 extends Thread {

    private String url;
    private String name;

    public TestThread2 (String url, String name) {
        this.url = url;
        this.name = name;
    }

    @Override
    public void run() {
        WebDownloader webDownloader = new WebDownloader();
        webDownloader.downloader(url, name);
        System.out.println("下载了文件名为：" + name);
    }

    public static void main(String[] args) {
        TestThread2 t1 = new TestThread2("https://t7.baidu.com/it/u=3616242789,1098670747&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg?sec=1604305362&t=1a625b5defe8f755e4594fc452b3ed42", "D:\\aaa\\1.jpg");
        TestThread2 t2 = new TestThread2("https://t8.baidu.com/it/u=1484500186,1503043093&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg?sec=1604305362&t=3f21340aa82abfc291ff2b83709fd2d2", "D:\\aaa\\2.jpg");
        TestThread2 t3 = new TestThread2("https://t9.baidu.com/it/u=1307125826,3433407105&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg?sec=1604305362&t=074b24ec6e2b06276071e5790abbfd0c", "D:\\aaa\\3.jpg");

        t1.start();
        t2.start();
        t3.start();
    }
}

//  下载器
class WebDownloader {
    //  下载方法
    public void downloader (String url, String name) {
        try {
            FileUtils.copyURLToFile(new URL(url), new File(name));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("IO异常，downloader方法出现问题");
        }
    }

}